home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / term / next.trm < prev    next >
Text File  |  1993-09-15  |  11KB  |  414 lines

  1. /*
  2.  * $Id: next.trm%v 3.50 1993/07/09 05:35:24 woo Exp $
  3.  *
  4.  */
  5.  
  6. /* GNUPLOT - next.trm */
  7. /*
  8.  * Copyright (C) 1991, 1992   
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software  is provided "as is" without express or implied warranty.
  21.  * 
  22.  * This file is included by ../term.c.
  23.  *
  24.  * This terminal driver supports:
  25.  *     next
  26.  *
  27.  * AUTHORS
  28.  *  Nick Strobel from Russell Lang's post.trm
  29.  * 
  30.  * send your comments or suggestions to (info-gnuplot@dartmouth.edu).
  31.  *
  32.  * The 'postscript' driver produces landscape output 10" wide and 7" high.  
  33.  * To get a smaller epsf output use 'set size 0.5,0.5', 
  34.  * 'set term postscript portrait', make only one plot per file
  35.  * and change the first line of the postscript file from
  36.  * '%!PS-Adobe-2.0' to '%!PS-Adobe-2.0 EPSF-2.0'
  37.  * To change font to Times-Roman and font size to 20pts use 
  38.  * 'set term postscript "Times-Roman" 20'.
  39.  */
  40.  
  41.  
  42. /* NeXT driver by Nick Strobel, strobel@phast.phys.washington.edu */
  43. #import <math.h>
  44. #import <dpsclient/dpsclient.h>
  45. #import <dpsclient/dpsNeXT.h>
  46. #import <stdlib.h>
  47. #import "../epsviewe.h"
  48.  
  49. DPSContext d;
  50. static void window_create(float width, float height);
  51. float width,height;    
  52. float xsize_orig,ysize_orig;  
  53.  
  54. char next_font[MAX_ID_LEN+1] = "Helvetica" ; /* name of font */
  55. int next_fontsize = 14;                     /* size of font in pts */
  56. TBOOLEAN next_portrait = FALSE;                 /* vertical page */
  57. TBOOLEAN next_color = FALSE;
  58. TBOOLEAN init_called = FALSE;
  59. TBOOLEAN initframe_called = FALSE;
  60.  
  61. int next_page=0;            /* page count */
  62. int next_path_count=0;     /* count of lines in path */
  63. int next_ang=0;            /* text angle */
  64. enum JUSTIFY next_justify=LEFT;    /* text is flush left */
  65.  
  66. char *NEXT_header[] = {
  67. "/vpt2 vpt 2 mul def\n",
  68. "/hpt2 hpt 2 mul def\n",
  69. /* flush left show */
  70. "/Lshow { currentpoint stroke moveto\n",
  71. "  0 vshift rmoveto show } def\n", 
  72. /* flush right show */
  73. "/Rshow { currentpoint stroke moveto\n",
  74. "  dup stringwidth pop neg vshift rmoveto show } def\n", 
  75. /* centred show */
  76. "/Cshow { currentpoint stroke moveto\n",
  77. "  dup stringwidth pop -2 div vshift rmoveto show } def\n", 
  78. /* Dash or Color Line */
  79. "/DL { Color {setrgbcolor [] 0 setdash pop}\n",
  80. " {pop pop pop 0 setdash} ifelse } def\n",
  81. /* Border Lines */
  82. "/BL { stroke gnulinewidth 2 mul setlinewidth } def\n",
  83. /* Axes Lines */
  84. "/AL { stroke gnulinewidth 2 div setlinewidth } def\n",
  85. /* Plot Lines */
  86. "/PL { stroke gnulinewidth setlinewidth } def\n",
  87. /* Line Types */
  88. "/LTb { BL [] 0 0 0 DL } def\n", /* border */
  89. "/LTa { AL [1 dl 2 dl] 0 setdash 0 0 0 setrgbcolor } def\n", /* axes */
  90. "/LT0 { PL [] 0 1 0 DL } def\n",
  91. "/LT1 { PL [4 dl 2 dl] 0 0 1 DL } def\n",
  92. "/LT2 { PL [2 dl 3 dl] 1 0 0 DL } def\n",
  93. "/LT3 { PL [1 dl 1.5 dl] 1 0 1 DL } def\n",
  94. "/LT4 { PL [5 dl 2 dl 1 dl 2 dl] 0 1 1 DL } def\n",
  95. "/LT5 { PL [4 dl 3 dl 1 dl 3 dl] 1 1 0 DL } def\n",
  96. "/LT6 { PL [2 dl 2 dl 2 dl 4 dl] 0 0 0 DL } def\n",
  97. "/LT7 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 1 0.3 0 DL } def\n",
  98. "/LT8 { PL [2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 2 dl 4 dl] 0.5 0.5 0.5 DL } def\n",
  99. "/M {moveto} def\n",
  100. "/L {lineto} def\n",
  101. "/P { stroke [] 0 setdash\n", /* Point */
  102. "  currentlinewidth 2 div sub moveto\n",
  103. "  0 currentlinewidth rlineto  stroke } def\n",
  104. "/D { stroke [] 0 setdash  2 copy  vpt add moveto\n", /* Diamond */
  105. "  hpt neg vpt neg rlineto  hpt vpt neg rlineto\n",
  106. "  hpt vpt rlineto  hpt neg vpt rlineto  closepath  stroke\n",
  107. "  P  } def\n",
  108. "/A { stroke [] 0 setdash  vpt sub moveto  0 vpt2 rlineto\n", /* Plus (Add) */
  109. "  currentpoint stroke moveto\n",
  110. "  hpt neg vpt neg rmoveto  hpt2 0 rlineto stroke\n",
  111. "  } def\n",
  112. "/B { stroke [] 0 setdash  2 copy  exch hpt sub exch vpt add moveto\n", /* Box */
  113. "  0 vpt2 neg rlineto  hpt2 0 rlineto  0 vpt2 rlineto\n",
  114. "  hpt2 neg 0 rlineto  closepath  stroke\n",
  115. "  P  } def\n",
  116. "/C { stroke [] 0 setdash  exch hpt sub exch vpt add moveto\n", /* Cross */
  117. "  hpt2 vpt2 neg rlineto  currentpoint  stroke  moveto\n",
  118. "  hpt2 neg 0 rmoveto  hpt2 vpt2 rlineto stroke  } def\n",
  119. "/T { stroke [] 0 setdash  2 copy  vpt 1.12 mul add moveto\n", /* Triangle */
  120. "  hpt neg vpt -1.62 mul rlineto\n",
  121. "  hpt 2 mul 0 rlineto\n",
  122. "  hpt neg vpt 1.62 mul rlineto  closepath  stroke\n",
  123. "  P  } def\n",
  124. "/S { 2 copy A C} def\n", /* Star */
  125. NULL
  126. };
  127.  
  128. #define NEXT_XOFF    50    /* page offset in pts */
  129. #define NEXT_YOFF    50
  130.  
  131. #define NEXT_XMAX 7200
  132. #define NEXT_YMAX 5040
  133.  
  134. #define NEXT_XLAST (NEXT_XMAX - 1)
  135. #define NEXT_YLAST (NEXT_YMAX - 1)
  136.  
  137. #define NEXT_VTIC (NEXT_YMAX/80)
  138. #define NEXT_HTIC (NEXT_YMAX/80)
  139.  
  140. #define NEXT_SC (10)                /* scale is 1pt = 10 units */
  141. #define    NEXT_LW (0.5*NEXT_SC)        /* linewidth = 0.5 pts */
  142.  
  143. #define NEXT_VCHAR (14*NEXT_SC)        /* default is 14 point characters */
  144. #define NEXT_HCHAR (14*NEXT_SC*6/10)
  145.  
  146. NEXT_options()
  147. {
  148.     extern struct value *const_express();
  149.     extern double real();
  150.  
  151.     if (!END_OF_COMMAND) {
  152.         if (almost_equals(c_token,"p$ortrait")) {
  153.             next_portrait=TRUE;
  154.             c_token++;
  155.         }
  156.         else if (almost_equals(c_token,"l$andscape")) {
  157.             next_portrait=FALSE;
  158.             c_token++;
  159.         }
  160.         else if (almost_equals(c_token,"d$efault")) {
  161.             next_portrait=FALSE;
  162.             next_color=FALSE;
  163.             strcpy(next_font,"Helvetica");
  164.             next_fontsize = 14;
  165.             c_token++;
  166.         }
  167.     }
  168.  
  169.     if (!END_OF_COMMAND) {
  170.         if (almost_equals(c_token,"m$onochrome")) {
  171.             next_color=FALSE;
  172.             c_token++;
  173.         }
  174.         else if (almost_equals(c_token,"c$olor")) {
  175.             next_color=TRUE;
  176.             c_token++;
  177.         }
  178.     }
  179.  
  180.     if (!END_OF_COMMAND && isstring(c_token)) {
  181.         quote_str(next_font,c_token);
  182.         c_token++;
  183.     }
  184.  
  185.     if (!END_OF_COMMAND) {
  186.         /* We have font size specified */
  187.         struct value a;
  188.         next_fontsize = (int)real(const_express(&a));
  189.         c_token++;
  190.         term_tbl[term].v_char = (unsigned int)(next_fontsize*NEXT_SC);
  191.         term_tbl[term].h_char = (unsigned int)(next_fontsize*NEXT_SC*6/10);
  192.     }
  193.  
  194.     sprintf(term_options,"%s %s \"%s\" %d",
  195.         next_portrait ? "portrait" : "landscape",
  196.         next_color ? "color" : "monochrome",next_font,next_fontsize);
  197. }
  198.  
  199.  
  200. NEXT_init()
  201. {
  202. struct termentry *t = &term_tbl[term];
  203. int i;
  204. float llx,lly,urx,ury;
  205. /* I commented out the postscript comment fields because they are not really 
  206. *  needed (even the bounding box). However, if you'd like to send all the
  207. *  postscript to the context d, they're still there for you
  208. */
  209.  
  210.         llx=NEXT_XOFF;
  211.         lly=NEXT_YOFF;
  212.  
  213.     if(!initframe_called) {
  214.         if(!next_portrait){
  215.             urx=(xsize*(NEXT_XMAX)/NEXT_SC+0.5+NEXT_XOFF);
  216.             ury=(ysize*(NEXT_YMAX)/NEXT_SC+0.5+NEXT_YOFF);
  217.         } else {
  218.             urx=(ysize*(NEXT_YMAX)/NEXT_SC+0.5+NEXT_XOFF);
  219.             ury=(xsize*(NEXT_XMAX)/NEXT_SC+0.5+NEXT_YOFF);
  220.         }
  221.           width=urx-llx;    
  222.           height=ury-lly;
  223.           xsize_orig=xsize;
  224.           ysize_orig=ysize;    
  225.           window_create(width,height);
  226.              d=DPSGetCurrentContext();
  227.        DPSPrintf(d,"/showpage {initgraphics} bind def\n");
  228.       }
  229.         DPSPrintf(d,"1 setgray\n");
  230.         DPSPrintf(d,"0 1 %g %g rectfill\n",rint(width),rint(height));
  231.         DPSPrintf(d,"0 setgray\n");
  232.         DPSPrintf(d,"0 1 %g %g rectstroke\n",rint(width),rint(height));
  233. //           DPSPrintf(d,"/_the_saved_vm_ save def\n");
  234.         DPSPrintf(d,"%g %g translate\n",-llx,-lly); /* KEEP this IN! */
  235.  
  236. //        DPSPrintf(d,"%%!PS-ADOBE-2.0 EPSF-2.0\n");
  237. //        DPSPrintf(d,"%%%%Creator: gnuplot\n");
  238. //        DPSPrintf(d,"%%%%DocumentFonts: %s\n", next_font);
  239. //        DPSPrintf(d,"%%%%BoundingBox: %d %d ", NEXT_XOFF,NEXT_YOFF);
  240. //        if (!next_portrait)
  241. //            DPSPrintf(d,"%d %d\n",(int)width, (int)height);
  242. //        else 
  243. //            DPSPrintf(d,"%d %d\n",(int)width, (int)height);
  244. //        DPSPrintf(d,"%%%%Pages: (atend)\n");
  245. //        DPSPrintf(d,"%%%%EndComments\n");
  246.         DPSPrintf(d,"/gnudict 40 dict def\ngnudict begin\n");
  247.         DPSPrintf(d,"/Color %s def\n",next_color ? "true" : "false");
  248.         DPSPrintf(d,"/gnulinewidth %.3f def\n",NEXT_LW);
  249.         DPSPrintf(d,"/vshift %d def\n", (int)(t->v_char)/(-3));
  250.         DPSPrintf(d,"/dl {%d mul} def\n",NEXT_SC); /* dash length */
  251.         DPSPrintf(d,"/hpt %.1f def\n",NEXT_HTIC/2.0);
  252.         DPSPrintf(d,"/vpt %.1f def\n",NEXT_VTIC/2.0);
  253.         for ( i=0; NEXT_header[i] != NULL; i++)
  254.             DPSPrintf(d,"%s",NEXT_header[i]);
  255.         DPSPrintf(d,"end\n");
  256. //        DPSPrintf(d,"%%%%EndProlog\n");
  257.         init_called=TRUE;
  258. }
  259.  
  260. static void window_create(float width, float height)
  261. {
  262.     initframe_called=TRUE;
  263.     
  264.     NXApp=[EpsViewer new];
  265.     [NXApp windowCreate:width Height:height];
  266. }
  267.  
  268. NEXT_graphics()
  269. {
  270. struct termentry *t = &term_tbl[term];
  271.     next_page++;
  272.     if(!init_called)
  273.         NEXT_init();    /* set up the frame properly */
  274.     init_called=FALSE;
  275. //    DPSPrintf(d,"%%%%Page: %d %d\n",next_page,next_page);
  276.     DPSPrintf(d,"gnudict begin\n");
  277.     DPSPrintf(d,"gsave\n");
  278.     DPSPrintf(d,"%d %d translate\n",NEXT_XOFF,NEXT_YOFF);
  279.     if (!next_portrait) {
  280. /* keep plot entirely in the window */
  281.         if(xsize>xsize_orig)
  282.             xsize=xsize_orig;
  283.         if(ysize>ysize_orig)
  284.             ysize=ysize_orig;
  285.         DPSPrintf(d,"%.3f %.3f scale\n", xsize/NEXT_SC, ysize/NEXT_SC);
  286.     }
  287.     else {
  288.         if(xsize>xsize_orig)
  289.             xsize=xsize_orig;
  290.         if(ysize>ysize_orig)
  291.             ysize=ysize_orig;
  292.         DPSPrintf(d,"%.3f %.3f scale\n", ysize/NEXT_SC, xsize/NEXT_SC);
  293.         DPSPrintf(d,"90 rotate\n0 %d translate\n", -NEXT_YMAX);
  294.     }
  295.     DPSPrintf(d,"0 setgray\n");
  296.     DPSPrintf(d,"/%s findfont %d ", next_font, (t->v_char) );
  297.     DPSPrintf(d,"scalefont setfont\n");
  298.     DPSPrintf(d,"newpath\n");
  299.     next_path_count = 0;    
  300. }
  301.  
  302.  
  303. NEXT_text()
  304. {
  305.     next_path_count = 0;
  306.     DPSPrintf(d,"stroke\ngrestore\nend\nshowpage\n");
  307. //    DPSPrintf(d,"\ngrestoreall _the_saved_vm_ restore\n");
  308.         
  309.     DPSFlushContext(d);
  310.     
  311.     
  312. }
  313.  
  314.  
  315. NEXT_reset()
  316. {    
  317.     DPSPrintf(d,"currentwindow termwindow\n");
  318.     DPSPrintf(d,"nulldevice\n");
  319.  
  320.     DPSFlushContext(d);
  321.     initframe_called=FALSE;
  322. }
  323.  
  324.  
  325. NEXT_linetype(linetype)
  326. int linetype;
  327. {
  328. char *line = "ba012345678"; 
  329.     DPSPrintf(d,"LT%c\n", line[(linetype%9)+2]);
  330.     next_path_count = 0;
  331. }
  332.  
  333.  
  334. NEXT_move(x,y)
  335. unsigned int x,y;
  336. {
  337.     DPSPrintf(d,"%d %d M\n", x, y);
  338.     next_path_count += 1;
  339. }
  340.  
  341.  
  342. NEXT_vector(x,y)
  343. unsigned int x,y;
  344. {
  345.     DPSPrintf(d,"%d %d L\n", x, y);
  346.     next_path_count += 1;
  347.     if (next_path_count >= 400) {
  348.         DPSPrintf(d,"currentpoint stroke moveto\n");
  349.         next_path_count = 0;
  350.     }
  351. }
  352.  
  353.  
  354. NEXT_put_text(x,y,str)
  355. unsigned int x, y;
  356. char *str;
  357. {
  358. char ch;
  359.     NEXT_move(x,y);
  360.     if (next_ang != 0)
  361.         DPSPrintf(d,"currentpoint gsave translate %d rotate 0 0 moveto\n"
  362.             ,next_ang*90);
  363. /*    NXPutc(psStream,'(');            */
  364.     DPSPrintf(d,"%c",'(');
  365.     ch = *str++;
  366.     while(ch!='\0') {
  367.         if ( (ch=='(') || (ch==')') || (ch=='\\') )
  368. /*            NXPutc(psStream,'\\');
  369.         NXPutc(psStream,ch);            */
  370.             DPSPrintf(d,"%c",'\\');
  371.         DPSPrintf(d,"%c",ch);
  372.         ch = *str++;
  373.     }
  374.     switch(next_justify) {
  375.         case LEFT : DPSPrintf(d,") Lshow\n");
  376.             break;
  377.         case CENTRE : DPSPrintf(d,") Cshow\n");
  378.             break;
  379.         case RIGHT : DPSPrintf(d,") Rshow\n");
  380.             break;
  381.     }
  382.     if (next_ang != 0)
  383.         DPSPrintf(d,"grestore\n");
  384.     next_path_count = 0;
  385. }
  386.  
  387. int NEXT_text_angle(ang)
  388. int ang;
  389. {
  390.     next_ang=ang;
  391.     return TRUE;
  392. }
  393.  
  394. int NEXT_justify_text(mode)
  395. enum JUSTIFY mode;
  396. {
  397.     next_justify=mode;
  398.     return TRUE;
  399. }
  400.  
  401. /* postscript point routines */
  402. NEXT_point(x,y,number)
  403. int x,y;
  404. int number;
  405. {
  406. char *point = "PDABCTS";
  407.     number %= POINT_TYPES;
  408.      if (number < -1)
  409.         number = -1;        /* negative types are all 'dot' */
  410.     DPSPrintf(d,"%d %d %c\n", x, y, point[number+1]);
  411.     next_path_count = 0;
  412. }
  413.  
  414.